package org.lifei.org.service.impl;

import org.lifei.base.service.impl.BaseServiceImpl;
import org.lifei.base.util.PageInfo;
import org.lifei.org.domain.Department;
import org.lifei.org.mapper.DepartmentMapper;
import org.lifei.org.query.Departmentquery;
import org.lifei.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper mapper;

    @Override
    public void insert(Department department) {
        mapper.insert(department);
        if (Objects.isNull(department.getParent().getId())){
            department.setPath("/"+department.getId());
        }else {
            Department department1 = mapper.queryOne(department.getParent().getId());
            department.setPath(department1.getPath()+"/"+department.getId());
        }
        mapper.update(department);
    }

    @Override
    public void update(Department department) {
        if (Objects.isNull(department.getParent().getId())){
            department.setPath("/"+department.getId());
        }else {
            Department department1 = mapper.queryOne(department.getParent().getId());
            department.setPath(department1.getPath()+"/"+department.getId());
        }
        mapper.update(department);
    }

    @Override
    public List<Department> DepTree() {
        return mapper.DepTree();
    }
}
